package pcjavanet.mobilesell.service;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;  
import java.util.Iterator; 
import java.util.Map;      
import pcjavanet.mobilesell.model.Catalog;
import pcjavanet.mobilesell.model.CatalogExample;
import pcjavanet.mobilesell.model.Unit;
import pcjavanet.mobilesell.model.UnitExample;
import pcjavanet.mobilesell.model.Configuration;
import pcjavanet.mobilesell.model.ConfigurationExample;
import pcjavanet.mobilesell.model.Goods; 
import pcjavanet.mobilesell.model.PageBean;
import pcjavanet.mobilesell.model.GoodsExample;
import pcjavanet.mobilesell.model.QryCondition;
import pcjavanet.mobilesell.model.QryTerm;
import pcjavanet.mobilesell.persistence.CatalogMapper;
import pcjavanet.mobilesell.persistence.UnitMapper;
import pcjavanet.mobilesell.persistence.ConfigurationMapper;
import pcjavanet.mobilesell.persistence.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class GoodsService {


  @Autowired
  private  GoodsMapper goodsMapper;
  @Autowired
  private  CatalogMapper catalogMapper;
  @Autowired
  private  UnitMapper unitMapper;
  @Autowired
  private  ConfigurationMapper configurationMapper;


  public int  insert( Goods goods ) throws Exception{
	return goodsMapper.insert (goods);
  }


  public Goods  selectByPrimaryKey( Goods  bean ) {
 	int dbid = bean.getDbid();
 	bean = goodsMapper.selectByPrimaryKey( dbid );
	Catalog catalog = catalogMapper.selectByPrimaryKey( bean.getRefCatalogDbid() );
	bean.setCatalog(catalog) ;
	Unit unit = unitMapper.selectByPrimaryKey( bean.getRefUnitDbid() );
	bean.setUnit(unit) ;
	Configuration configuration = configurationMapper.selectByPrimaryKey( bean.getRefConfigurationDbid() );
	bean.setConfiguration(configuration) ;
	return bean ;
  }


  public int  updateByPrimaryKeySelective( Goods goods ) throws Exception{
	return goodsMapper.updateByPrimaryKeySelective( goods );
  }


  public int  deleteByPrimaryKey( Goods  bean ) throws Exception{
 	int dbid = bean.getDbid();
	return goodsMapper.deleteByPrimaryKey( dbid );
  }


  public List<Goods>  selectByExample( Goods goods ) throws Exception{
    GoodsExample example = new GoodsExample();
	return goodsMapper.selectByExample( example );
  }

  public List<Goods>  getOnePageBeans( QryCondition condition , PageBean pageBean ) throws Exception{
    GoodsExample example = new GoodsExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	List<Goods> ls=goodsMapper.getOnePageBeans( example , pageBean );
//begin
	if ( ls.size() > 0 ) {
  		Map<Integer,Integer> catalogIdmap = new HashMap<Integer,Integer>();
  		for(Goods  m : ls){
  			m.setCatalog(new Catalog ()) ; 
  			if ( m.getRefCatalogDbid() != null ) 
  				catalogIdmap.put(m.getRefCatalogDbid(), m.getRefCatalogDbid());
  		};
  		List<Integer> refCatalogDbids = new ArrayList<Integer>();
  		Iterator<Integer> refCatalogDbidit = catalogIdmap.keySet().iterator(); 
  		while ( refCatalogDbidit.hasNext() ){
  			refCatalogDbids.add( refCatalogDbidit.next() );
  		}
  		CatalogExample catalogExample = new CatalogExample();
  		if ( refCatalogDbids.size() > 0 ) 
  			catalogExample.createCriteria().andDbidIn( refCatalogDbids );
  		List<Catalog> catalogs = catalogMapper.selectByExample( catalogExample);
  		for(Goods m : ls ){
  			for(Catalog c : catalogs ){
  				if ( m.getRefCatalogDbid() !=null && c.getDbid() !=null && m.getRefCatalogDbid().intValue() == c.getDbid().intValue() ) {
  					m.setCatalog( c );
  				}
  			}
  		}
  		Map<Integer,Integer> unitIdmap = new HashMap<Integer,Integer>();
  		for(Goods  m : ls){
  			m.setUnit(new Unit ()) ; 
  			if ( m.getRefUnitDbid() != null ) 
  				unitIdmap.put(m.getRefUnitDbid(), m.getRefUnitDbid());
  		};
  		List<Integer> refUnitDbids = new ArrayList<Integer>();
  		Iterator<Integer> refUnitDbidit = unitIdmap.keySet().iterator(); 
  		while ( refUnitDbidit.hasNext() ){
  			refUnitDbids.add( refUnitDbidit.next() );
  		}
  		UnitExample unitExample = new UnitExample();
  		if ( refUnitDbids.size() > 0 ) 
  			unitExample.createCriteria().andDbidIn( refUnitDbids );
  		List<Unit> units = unitMapper.selectByExample( unitExample);
  		for(Goods m : ls ){
  			for(Unit c : units ){
  				if ( m.getRefUnitDbid() !=null && c.getDbid() !=null && m.getRefUnitDbid().intValue() == c.getDbid().intValue() ) {
  					m.setUnit( c );
  				}
  			}
  		}
  		Map<Integer,Integer> configurationIdmap = new HashMap<Integer,Integer>();
  		for(Goods  m : ls){
  			m.setConfiguration(new Configuration ()) ; 
  			if ( m.getRefConfigurationDbid() != null ) 
  				configurationIdmap.put(m.getRefConfigurationDbid(), m.getRefConfigurationDbid());
  		};
  		List<Integer> refConfigurationDbids = new ArrayList<Integer>();
  		Iterator<Integer> refConfigurationDbidit = configurationIdmap.keySet().iterator(); 
  		while ( refConfigurationDbidit.hasNext() ){
  			refConfigurationDbids.add( refConfigurationDbidit.next() );
  		}
  		ConfigurationExample configurationExample = new ConfigurationExample();
  		if ( refConfigurationDbids.size() > 0 ) 
  			configurationExample.createCriteria().andDbidIn( refConfigurationDbids );
  		List<Configuration> configurations = configurationMapper.selectByExample( configurationExample);
  		for(Goods m : ls ){
  			for(Configuration c : configurations ){
  				if ( m.getRefConfigurationDbid() !=null && c.getDbid() !=null && m.getRefConfigurationDbid().intValue() == c.getDbid().intValue() ) {
  					m.setConfiguration( c );
  				}
  			}
  		}
	}
//end
	return ls;
  }

  public int getTotalRecords( QryCondition condition , PageBean pageBean ) throws Exception{
    GoodsExample example = new GoodsExample();
    ServiceUtil.dealCriteria( condition,  example.createCriteria() );
	return goodsMapper.getTotalRecords( example , pageBean );
  }

  public int countByExample( Goods goods , PageBean pageBean ){
    GoodsExample example = new GoodsExample();
	return goodsMapper.countByExample( example );
  }
  public List<Goods> selectByExamplePage( Goods goods , PageBean pageBean ){
  GoodsExample example = new GoodsExample();
	return goodsMapper.selectByExamplePage( example , pageBean );
  }

}
